package muview;

import mumethod.AccessSchools;
import mumethod.AccessStudents;
import mumethod.AccessUsers;
import muobject.User;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.springframework.context.ApplicationContext;

import com.cloudgarden.resource.SWTResourceManager;

public class DialogEditAccount extends Dialog{

	private Shell shell = null;
	private GridLayout gridLayout = null;
	private GridData gridData = null;
	private Label lab_accountold;
	private Label lab_account;
	private Label lab_password;
	private Text txt_account;
	private Text txt_password;
	private Button but_confirm;
	
	private AccessUsers getUsers;

	private User user;
	
	DialogEditAccount(Shell parent, ApplicationContext springConnector) {
		super(parent);
		getUsers = (AccessUsers) springConnector.getBean("AccessUsersService");
	}
	
	public boolean open() {
		user = getUsers.getTopUser();
		Shell parent = getParent();
		shell = new Shell(parent, SWT.TITLE | SWT.CLOSE | SWT.BORDER);
		shell.setText("登入密碼");
		shell.setBounds(200, 200, 350, 160);
		gridLayout = new GridLayout();
		gridLayout.numColumns = 4;
		shell.setLayout(gridLayout);
		{
			lab_accountold = new Label(shell, SWT.NONE);
			GridData lab_accountoldLData = new GridData();
			lab_accountoldLData.horizontalSpan = 4;
			lab_accountold.setText("帳號 : " + user.getAccount());
			lab_accountold.setFont(SWTResourceManager.getFont("Tahoma", 16, 1, false, false));
			lab_accountold.setLayoutData(lab_accountoldLData);
		}
		{
			lab_account = new Label(shell, SWT.NONE);
			GridData lab_accountLData = new GridData();
			lab_accountLData.horizontalSpan = 1;
			lab_account.setText("新帳號   : ");
			lab_account.setFont(SWTResourceManager.getFont("Tahoma", 12, 1, false, false));
			lab_account.setLayoutData(lab_accountLData);
		}
		{
			txt_account = new Text(shell, SWT.BORDER);
			GridData txt_accountLData = new GridData();
			txt_accountLData.widthHint = 200;
			txt_accountLData.horizontalSpan = 3;
			txt_account.setSize(30, 200);
			txt_account.setText(user.getAccount());
			txt_account.setFont(SWTResourceManager.getFont("Tahoma", 12, 1, false, false));
			txt_account.setLayoutData(txt_accountLData);
		}
		{
			lab_password = new Label(shell, SWT.NONE);
			GridData lab_passwordLData = new GridData();
			lab_passwordLData.horizontalSpan = 1;
			lab_password.setText("新密碼   : ");
			lab_password.setFont(SWTResourceManager.getFont("Tahoma", 12, 1, false, false));
			lab_password.setLayoutData(lab_passwordLData);
		}
		{
			txt_password = new Text(shell, SWT.BORDER);
			GridData txt_passwordLData = new GridData();
			txt_passwordLData.widthHint = 200;
			txt_passwordLData.horizontalSpan = 3;
			txt_password.setSize(30, 200);
			txt_password.setText(user.getPassword());
			txt_password.setFont(SWTResourceManager.getFont("Tahoma", 12, 1, false, false));
			txt_password.setLayoutData(txt_passwordLData);
		}
		{
			but_confirm = new Button(shell, SWT.None);
			GridData but_confirmLData = new GridData();
			but_confirmLData.widthHint = 100;
			but_confirmLData.horizontalSpan = 4;
			but_confirm.setText("確定修改");
			but_confirm.setFont(SWTResourceManager.getFont("Tahoma", 12, 1, false, false));
			but_confirm.setLayoutData(but_confirmLData);
			but_confirm.addSelectionListener(new SelectionAdapter() {
				public void widgetSelected(SelectionEvent e) {
					if(getUsers.setAccountPassword(txt_account.getText(), txt_password.getText(), user.getUid())){
						shell.close();
					}else{
						DialogMessage.open(shell, "修改帳號失敗", "修改帳號失敗",
								SWT.ICON_WARNING);
					}
				}
			});
		}
		
		shell.open();
		Display display = parent.getDisplay();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}
		
		return true;
	}
}
